<?php
namespace app\http\middleware;
use app\Request;
use think\Response;

/**
 * 跨域中间件
 * Class AllowOriginMiddleware
 * @package app\http\middleware
 */
class AllowOrigin
{
    /**
     * 允许跨域的域名
     * @var string
     */
    protected $cookieDomain;

    /**
     * @param Request $request
     * @param \Closure $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
    	header('Access-Control-Allow-Origin:*');     
		header('Access-Control-Allow-Methods:*');  
		header('Access-Control-Allow-Headers:content-type,authorization,x-token,x-shop,x-sc-token');
		header('Access-Control-Allow-Credentials:false');
        if ($request->method(true) == 'OPTIONS') {
            $response = Response::create('ok')->code(200)->header();
        } else {
            $response = $next($request);
        }
		
        return $response;		
    }
}